#!/bin/bash
# Program:
#       Generate a line chart according to the .meminfo data
# History:
#       2012/09/26  Version: 0.0.6
# Change:
#       1. Show total running time.
#       2. Show the device IMEI.
FILENAME=$1
if [ ! -f "${FILENAME}" ]; then
    echo "Usage: $0 [MEMINFO FILE]"
    exit
fi
TMP='$1.tmp'
DEVICE=`echo ${FILENAME} | xargs -n 1 basename | cut -d '_' -f 2 | cut -d '.' -f 1`
PROCESS=`echo ${FILENAME} | xargs -n 1 basename | cut -d '_' -f 1`
IMEI=`grep 'IMEI' ${FILENAME} | awk '{print $2}'`
TOTAL_TIME=`grep 'TotalTime' ${FILENAME} | awk '{print $2}'`


# Generate required data format from the input file.
chmod 666 ${FILENAME}
cat $1 | tr -d '\r' | tr -d '\\' > $TMP
mv ${TMP} ${FILENAME}
cat ${FILENAME} | grep Native | awk '{print $2 " " $3 " " $4 " " $5 " " $6 " " $7}' > native.txt
cat ${FILENAME} | grep Dalvik | awk '{print $2 " " $3 " " $4 " " $5 " " $6 " " $7}' > dalvik.txt
cat ${FILENAME} | grep '\[SYSTEM\]' | awk {'print $2 " " $3'} > time.txt
echo '##date time Pss Shared_Dirty Private_Dirty Heap_Size Heap_Alloc Heap_Free' > native.dat
paste -d ' ' time.txt native.txt >> native.dat
echo '##date time Pss Shared_Dirty Private_Dirty Heap_Size Heap_Alloc Heap_Free' > dalvik.dat
paste -d ' ' time.txt dalvik.txt >> dalvik.dat


# Generate the line chart from native.dat
/usr/bin/gnuplot <<EOC
reset
#set terminal png enhanced size 700,400 font "/usr/share/fonts/truetype/takao/TakaoExMincho.ttf" linewidth 0.5
set terminal png enhanced size 700,400 
set output '${DEVICE}_native.png' 
set title "Memory Information of ${PROCESS} in ${DEVICE}\nIMEI: ${IMEI}\nTotalTime: ${TOTAL_TIME}"
set xlabel 'Time' rotate 
set ylabel 'KB' 
set xtics out offset character 0,-1.5 rotate by 45

set xdata time
set timefmt "%m-%d %H:%M:%S"
set format x "%H:%M"

set key reverse Left outside
set keytitle "Native"
set grid

#set style data linespoints
set style data lines

plot "native.dat" using 1:3 title "Pss" with points lc rgb "dark-gray" pt 7, \
"" using 1:4 title "Shared Dirty" with line lc rgb "pink" lw 2, \
"" using 1:5 title "Private Dirty" with line lc rgb "gold" lw 2, \
"" using 1:6 title "Heap Size" with line lc rgb "skyblue" lw 2, \
"" using 1:7 title "Heap Alloc" with line lc rgb "orange-red" lw 3 , \
"" using 1:8 title "Heap Free" with line lc rgb "sea-green" lw 2
#
EOC


# Generate the line chart from dalvik.dat
/usr/bin/gnuplot <<EOC
reset
#set terminal png enhanced size 700,400 font "/usr/share/fonts/truetype/takao/TakaoExMincho.ttf" linewidth 0.5
set terminal png enhanced size 700,400 
set output '${DEVICE}_dalvik.png' 
set title "Memory Information of ${PROCESS} in ${DEVICE}\nIMEI: ${IMEI}\nTotalTime: ${TOTAL_TIME}"
set xlabel 'Time' rotate 
set ylabel 'KB' 
set xtics out offset character 0,-1.5 rotate by 45

set xdata time
set timefmt "%m-%d %H:%M:%S"
set format x "%H:%M"

set key reverse Left outside
set keytitle "Dalvik"
set grid

#set style data linespoints
set style data lines

plot "dalvik.dat" using 1:3 title "Pss" with points lc rgb "dark-gray" pt 7, \
"" using 1:4 title "Shared Dirty" with line lc rgb "pink" lw 2, \
"" using 1:5 title "Private Dirty" with line lc rgb "gold" lw 2, \
"" using 1:6 title "Heap Size" with line lc rgb "skyblue" lw 2, \
"" using 1:7 title "Heap Alloc" with line lc rgb "orange-red" lw 3 , \
"" using 1:8 title "Heap Free" with line lc rgb "sea-green" lw 2
#
EOC
